//
//  ProfileViewModel.swift
//  MoyoungMy
//
//  Created by 易先生 on 2022/8/12.
//

import UIKit
import RxSwift
import RxCocoa
import MYModule
import Differentiator

class ProfileViewModel: NSObject {

	override init() {
		super.init()
		getItems()
	}

	public let items = BehaviorRelay<[ProfileCellViewModel]>(value: [])

	public func getItems() {
		var cells: [ProfileCellViewModel] = []

        let profile: UserModel = UserCacheManagement.userInfo()
        let unitType = profile.unitType
        
        let heightUnit = unitType == .metric ? "unit_cm".language : "in"

        var stepLength = "\(profile.steplength)\(heightUnit)"
        var height = "\(profile.height)\(heightUnit)"
        
        if unitType == .imperial {
            stepLength = "\(profile.steplength/12)ft\(profile.steplength%12)\(heightUnit)"
            height = "\(profile.height/12)ft\(profile.height%12)\(heightUnit)"
        }
        
        let sexCellModel =  ProfileCellViewModel(title: "info_setting_gender".language, type: .sex)
        sexCellModel.value.accept(profile.sex.localeName)
		cells.append(sexCellModel)

		let heightCellModel = ProfileCellViewModel(title: "info_setting_height".language, type: .height)
        heightCellModel.value.accept(height)
		cells.append(heightCellModel)

		let weightUnit = unitType == .metric ? "unit_kg".language : "lb"
		let weightCellModel = ProfileCellViewModel(title: "info_setting_weight".language, type: .weight)
        weightCellModel.value.accept("\(profile.weight)\(weightUnit)")
        cells.append(weightCellModel)

        let birthCellModel = ProfileCellViewModel(title: "info_setting_birthday".language, type: .birth)
        let date = Date(timeIntervalSince1970: TimeInterval(profile.birth))
        let birthTime = date.toFormat("year_month_day_format".language)
        birthCellModel.value.accept(birthTime)
        cells.append(birthCellModel)
        
        let stepLengthCellModel = ProfileCellViewModel(title: "info_setting_step_length".language, type: .steplength)
        stepLengthCellModel.value.accept(stepLength)
        cells.append(stepLengthCellModel)

		items.accept(cells)
	}

	public func setDefaultProfile() {
        
        let year = Date().year - 20
        let timeStamp = "\(year)-\(01)-\(01)".toDate()?.timeIntervalSince1970
        let birth = Int(timeStamp ?? 0)
		if getCurrentLanguageIsUS() {
            
            let profile = UserModel(unitType: .metric, warningMxHR: 160, sitRemind: false, lowBatteryReminder: true, weight: 144, height: 56, birth: birth, steplength: 22, sex: .male)
            UserCacheManagement.updateUserInfo(profile)
            
		} else {
            let profile = UserModel(unitType: .metric, warningMxHR: 160, sitRemind: false, lowBatteryReminder: true, weight: 65, height: 170, birth: birth, steplength: 67, sex: .male)
            UserCacheManagement.updateUserInfo(profile)
		}
		getItems()
	}
    
    private func getCurrentLanguageIsUS() -> Bool{
        var isUS = false
        if let preferredLang = Bundle.main.preferredLocalizations.first {
            if preferredLang == "en_US" {
                isUS = true
            }
        }
        
        return isUS
    }
}
